package com.tian.framework.security.handler;

import com.tian.framework.security.domian.SecurityResult;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description: 自定义登录成功Handler，返回json
 * @Author: tian
 * @Date: 2019-12-12 16:00:00
 */
@Component
public class LoginSuccessHandlerImpl implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter out = httpServletResponse.getWriter();
        SecurityResult ajaxResult = new SecurityResult();
        out.write(ajaxResult.success("登录成功"));
        out.flush();
        out.close();
    }
}
